package com.zl.tcbnewcar.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zl.tcbnewcar.R;
import com.zl.tcbnewcar.base.BaseActivity;
import com.zl.tcbnewcar.model.CarUserEntity;
import com.zl.tcbnewcar.utils.API;
import com.zl.tcbnewcar.utils.HttpUtils;
import com.zl.tcbnewcar.utils.ImageLoader;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author zhanglei
 * @date 18/4/9
 */
public class PersonDetailActivity extends BaseActivity {

    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.iv_title_share)
    ImageView ivTitleShare;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.civ_user_icon)
    ImageView civUserIcon;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    @BindView(R.id.tv_user_phone)
    TextView tvUserPhone;
    @BindView(R.id.tv_user_real)
    TextView tvUserReal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_detail);
        ButterKnife.bind(this);
        initView();
        initData();
        initListener();
    }

    private void initData() {
        int uid = getIntent().getIntExtra("uid", 0);
        Map<String, String> params = new HashMap<>();
        params.put("uid", uid + "");
        HttpUtils.getInstance().Post(mActivity, params, API.getUserInfo, new HttpUtils.OnOkHttpCallback() {
            @Override
            public void onSuccess(String body) {
                CarUserEntity entity = new Gson().fromJson(body, CarUserEntity.class);
                updateUi(entity);
            }

            @Override
            public void onError(Request error, Exception e) {

            }
        });

        OkHttpClient mOkClient = new OkHttpClient.Builder().build();
        FormBody.Builder builder = new FormBody.Builder();
        if (params != null) {
            Set<Map.Entry<String, String>> entries = params.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                builder.add(entry.getKey(), entry.getValue());
            }
        }
        //创建请求体
        Request request = new Request.Builder().url(API.getApproverInfo).post(builder.build()).build();

        //加入任务调度
        mOkClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(final Call call, final IOException e) {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showToast("网络访问错误，请联系管理员处理");
                    }
                });
            }

            @Override
            public void onResponse(final Call call, final Response response) throws IOException {
                final String string = response.body().string();
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject jsonObject = new JSONObject(string);
                            boolean result = jsonObject.optBoolean("result");
                            if (result) {
                                tvUserReal.setText("已实名认证");
                            } else {
                                tvUserReal.setText("还未实名认证");
                            }

                        } catch (Exception e) {

                        }
                    }
                });
            }
        });

    }

    /**
     * 获得到了数据，更新Ui
     *
     * @param entity
     */
    private void updateUi(CarUserEntity entity) {
        ImageLoader.loadRoundImageUrl(mActivity, entity.getUserImg(), civUserIcon);
        tvNickName.setText(entity.getUserNikeName());
        tvUserPhone.setText(entity.getUserPhone());
        Integer userSex = entity.getUserSex();
        if (userSex == 1) {
            tvSex.setText("男");
        } else {
            tvSex.setText("女");
        }
    }

    private void initListener() {

    }

    private void initView() {
        tvTitleName.setText("个人名片");
    }

    @OnClick({R.id.iv_title_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //返回
            case R.id.iv_title_back:
                finish();
                break;
        }
    }

}
